home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_014 / dimensions / thre.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  235 lines

  1. /*  This program will display THREE dimensions on a TWO dimensional screen  */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/exec.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfx.h>
  9. #include <graphics/text.h>
  10. #include <graphics/regions.h>
  11. #include <graphics/copper.h>
  12. #include <graphics/gels.h>
  13. #include <devices/serial.h>
  14. #include <devices/keymap.h>
  15. #include <hardware/blit.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <math.h>
  19. #ifdef LATTICE        /* Currently only Lattice has this */
  20. #include <limits.h>
  21. #endif
  22.  
  23. #define pi            3.141592654
  24. #define INTUITION_REV 1
  25. #define GRAPHICS_REV  1
  26. #define MILLION       1000000
  27. #define SX            320
  28. #define SY            200
  29.  
  30. struct IntuitionBase *IntuitionBase;
  31. struct GfxBase       *GfxBase;
  32. struct Screen        *Screen;
  33. struct Window        *Window;
  34. struct IntuiMessage  *Message;
  35.  
  36. struct NewScreen NewScreen =
  37.  {
  38.     0,0,SX,SY,1,-1,-2,/* HIRES | LACE*/ NULL, CUSTOMSCREEN,
  39.     NULL,NULL,NULL,NULL,
  40.  };
  41. struct NewWindow NewWindow =
  42.  {
  43.     0,00,SX,SY,-1,-1,
  44.     CLOSEWINDOW | MOUSEMOVE | MOUSEBUTTONS,
  45.     BACKDROP | BORDERLESS | WINDOWCLOSE,
  46.     NULL,NULL,NULL,NULL,NULL,
  47.     0,0,0,0,CUSTOMSCREEN,
  48.  };
  49.  
  50. /* This is the data all the functions need to see */
  51.  
  52. #define TOP 50 /* 100 3d points maximum, change at will       */
  53. #define WID 4   /* Width of the 3d matrix x,y,z, perspective   */
  54. #define DEW 2   /* Width of display data  x,y                  */
  55. #define PLA 3   /* THREE Planes in 3 dimensions                  */
  56. #define SIZ 100 /* Size of each line of capture at maximum     */
  57.  
  58. static double thre [ WID ][ WID ];/* This matrix will contain the 3d formula */
  59. static double deta [ TOP ][ WID ];/* This matrix will contain your data      */
  60.          int disp [ TOP ][ WID ];/* Screen coords, note use of WID not DEW  */
  61. static float rads [ PLA ]       ;/* Three rotation planes in three dimensions! */
  62.  
  63. /*********** RETRIEVE DATA FUNCTION ************/
  64.  
  65. retrieve (file)
  66. char *file[];
  67. {
  68.      FILE *input;
  69.      char buffer [ SIZ ],rec [ 20 ]; /* 80 chars on one line, 3 sets */
  70.      int a,i,pos;
  71.      pos = FALSE; /* if it stays false program will abort later */
  72.                   /* else it will represent size of data        */
  73.  
  74.      if ((input = fopen(file,"r"))== NULL) goto HEAVEN;
  75.  
  76.      while ((fgets(buffer,SIZ,input)))
  77.   {
  78.           a = 0;
  79.           for (i=0;i<WID;i++)
  80.        {
  81.                if (pos > TOP) goto HEAVEN;
  82.                a = strcspn(buffer,",");
  83.                strncpy (rec,buffer,a);
  84.                deta [pos][i] = atof (rec);
  85.                ++a;
  86.                strcpy (buffer,buffer+a);
  87.        }
  88.      ++pos;
  89.   }
  90. HEAVEN:
  91.  
  92.      fclose (input);
  93.      return (pos-1);
  94. }
  95.  
  96. /*********** MULTIPLY DATA FUNCTION ************/
  97.  
  98. multiply (data,P1)
  99. int data;
  100. double P1;
  101. {
  102.     int c,r,i;
  103.     float x,y,z;
  104.     double tweedle;
  105.     float Cz,Cy,Cx,Sz,Sy,Sx,Nz,Ny,Nx;
  106.  
  107. /* pull desired rotation radians out of array */
  108.     x = rads [ 0 ];
  109.     y = rads [ 1 ];
  110.     z = rads [ 2 ];
  111.  
  112.     Cz=cos(z);Sz=sin(z);Nz=-sin(z);
  113.     Cy=cos(y);Sy=sin(y);Ny=-sin(y);
  114.     Cx=cos(x);Sx=sin(x);Nx=-sin(x);
  115.     
  116.     thre [0][0] = Cz*Cy;
  117.     thre [0][1] = Sz*Cx+Cz*Sy*Nx;
  118.     thre [0][2] = 0.0;
  119.     thre [0][3] = (Sz*Sx+Cz*Sy*Cx)*P1;
  120.  
  121.     thre [1][0] = Nz*Cy;
  122.     thre [1][1] = Cz*Cx+Nz*Sy*Nx;
  123.     thre [1][2] = 0.0;
  124.     thre [1][3] = (Cz*Sx+Nz*Sy*Cx)*P1;
  125.  
  126.     thre [2][0] = Ny;
  127.     thre [2][1] = Cy*Nx;
  128.     thre [2][2] = 0.0;
  129.     thre [2][3] = Cy*Cx*P1;
  130.  
  131.     thre [3][0] = 0.0;
  132.     thre [3][1] = 0.0;
  133.     thre [3][2] = 0.0;
  134.     thre [3][3] = 1.0;
  135.  
  136. /* Cross multipy transformation matrix agaist your points */
  137.  
  138.     for (r=0;r<data;r++)
  139.  {
  140.          for (c=0;c<WID;c++)
  141.       {
  142.               tweedle = 0.0;
  143.               for (i=0;i<WID;i++)
  144.            {
  145.  
  146.                    tweedle += (deta [r][i]) * (thre [i][c]);
  147.            }
  148.               disp [r][c] = ceil(tweedle);
  149.       }
  150.  }
  151. return (TRUE);
  152. }
  153.  
  154. main (argc,argv)
  155. short argc;
  156. char *argv[];
  157.  
  158. {
  159.  
  160.      double persx;
  161.      short forever = TRUE;
  162.      int x,y,pos,top,rot,i;
  163.  
  164.      short INTOPEN = FALSE,
  165.            GFXOPEN = FALSE,
  166.            SCNOPEN = FALSE,
  167.            WINOPEN = FALSE,
  168.            MENOPEN = FALSE;
  169.  
  170.      rads[0]=0.0;rads[1]=0.0;rads[2]=0.0;
  171.  
  172.      if (argc == 1) { printf("Please supply rotatation's. ex: 1> thre 7\n");exit();}
  173.  
  174.      rot = atoi(argv[1]);
  175.  
  176.      if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",INTUITION_REV)))
  177.          goto HELL;INTOPEN = TRUE;
  178.  
  179.      if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",GRAPHICS_REV)))
  180.          goto HELL;GFXOPEN = TRUE;
  181.  
  182.      if (!(Screen = (struct Screen *)OpenScreen(&NewScreen)))
  183.          goto HELL;SCNOPEN = TRUE;
  184.  
  185.      ShowTitle(Screen,FALSE);
  186.      NewWindow.Screen = Screen;
  187.  
  188.      if (!(Window = (struct Window *)OpenWindow(&NewWindow)))
  189.          goto HELL;WINOPEN = TRUE;
  190.  
  191. /* Start of Program */
  192.  
  193.      if (!(top = retrieve ("thredata"))) goto HELL;
  194.  
  195. /* Main Program Loop */
  196.  
  197.     while (forever)
  198.  {
  199.         if (Message = (struct IntuiMessage *)GetMsg(Window->UserPort))
  200.             if ((Message->Class == CLOSEWINDOW)) goto HELL;
  201.  
  202.  
  203.     if ((rot & 1) > 0) rads[0] +=.1;if (rads[0]>pi)rads[0]=-pi;
  204.     if ((rot & 2) > 0) rads[1] +=.1;if (rads[1]>pi)rads[1]=-pi;
  205.     if ((rot & 4) > 0) rads[2] +=.1;if (rads[2]>pi)rads[2]=-pi;
  206.  
  207.     persx = 1.0;
  208.     if (!(multiply (top,persx))) goto HELL;
  209.     SetAPen  (Window->RPort,0);
  210.     RectFill (Window->RPort,0,10,SX,SY);
  211.     SetAPen  (Window->RPort,3);
  212.  
  213.     for (pos=0;pos<top;pos++)
  214.   {
  215.         x = disp [ pos ][ 0 ];
  216.         y = disp [ pos ][ 1 ];
  217.         x += SX/2;y += SY/2;
  218.  
  219.         if (pos == 0)
  220.         Move (Window->RPort,x,y);
  221.  
  222.         Draw (Window->RPort,x,y);
  223.   }
  224.        
  225.  }
  226. HELL:
  227.  
  228.       if (MENOPEN)  ClearMenuStrip (Window);
  229.       if (WINOPEN)  CloseWindow    (Window);
  230.       if (SCNOPEN)  CloseScreen    (Screen);
  231.       if (GFXOPEN)  CloseLibrary   (GfxBase);
  232.       if (INTOPEN)  CloseLibrary   (IntuitionBase);
  233.       exit (TRUE);
  234. }
  235.